﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ROrganizer.Client.Files
{
    public static class FileEnumerator
    {
        public static IEnumerable<string> EnumerateFiles(string[] paths)
        {
            foreach (var path in paths)
                foreach (var file in FileEnumerator.EnumerateFiles(path))
                    yield return file.ToLower();
        }

        private static IEnumerable<string> EnumerateFiles(string path)
        {
            if (File.Exists(path) && ApplicationSettings.SupportedExtensions.Contains(Path.GetExtension(path).ToLower()))
                yield return path.ToLower();

            if (Directory.Exists(path))
                foreach (var entry in Directory.EnumerateFileSystemEntries(path))
                    foreach (var file in FileEnumerator.EnumerateFiles(entry))
                        yield return file;
        }

        public static long GetFileSize(params string[] paths)        
        {
            if (paths.Length == 0)
                return 0L;

            var query = from path in paths
                        select new FileInfo(path).Length;

            return query.Sum();
        }

        public static DateTime GetFileTimestmap(string path)
        {
            return new FileInfo(path).LastWriteTimeUtc.ToUniversalTime();
        }
    }
}
